package cn.edu.fudan.classifier;

import cn.edu.fudan.data.ExtractFeature;
import cn.edu.fudan.data.ReadData;
import cn.edu.fudan.tools.ClassifierSummary;
import cn.edu.fudan.tools.GetConfig;
import cn.edu.fudan.type.Config;
import cn.edu.fudan.type.DataItem;
import cn.edu.fudan.type.Feature;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class ExplosionData {
	private static Logger logger = Logger.getLogger(ExplosionData.class);

	public void testOnExplosion() throws Exception {
		Config config = new GetConfig().getConfig();
		logger.info(
				"==================================================================================================================");
		logger.info(config);
		logger.info(
				"===================================================================================================================");
		ReadData readData = new ReadData();
		ExtractFeature extractFeature = new ExtractFeature();
		ClassifierSummary classifierSummary = new ClassifierSummary();


		String path = config.getPath();

		int[] datasets = new int[] { 103,148,183,260,293,424,456,511,721,850,878,952,1168,1468,1570,1788,1814,1874,1881,1896,2064,2067,2108,2240,2433,2469,2484,2664,2760,2814,2988,3011,3067,3130,3207,3275,3305,3306,3534,3539,3541,3551,3577,3606,3698,3718,3741,3844,3961,3982,4001,4022,4303,4304,4318,4564,4611,4677,4810,4907,4955,5042,5166,5481,5911,5943,5946,5947,5949,5988,6008,6064,6102,6118,6119,6121,6157,6283,6302,6328,6344,6385,6405,6435,6514,6582,6594,6604,6709,6768,6775,6779,6791,6799,6801,6862,6920,6946,6962,6971,6976,6998,7053,7128,7241,7265,7288,7310,7316,7369,7387,7414,7417,7474,7476,7520,7530,7533,7537,7541,7543,7553,7567,7570,7574,7664,7665,7715,7740,7765,7770,7794,7799,7888,7889,7899,7900,7903,7909,7910,7911,8076,8256,8273,8325,8330,8540,8572,8669,8832,8983,9145,9252,9307,9825,9831,10184,10269,10831,11063,11130,11136,11313,11345,11512,11518,11830,11868,11960,11962,12192,12515,12555,12708,12721,12764,12807,12825,12987,13014,13113,13665,13684,13845,13847,13866,13939,13962,14108,14197,14285,14330,14364,14663,14790,14798,14805,14930,15201,15313
 };

		List<DataItem> rawdatas = new ArrayList<>();

		for (int i = 0; i < datasets.length; i++) {
			String filePath = path + "data\\" + datasets[i];
			List<DataItem> rawdata = readData.readDataFromFile(filePath, true);
			rawdatas.addAll(rawdata);
		}

		String trainPath = path + "train.txt";
		List<List<Long>> trainpoints = readData.readTimePoint(trainPath, false);
		String testPath = path + "test.txt";
		List<List<Long>> testpoints = readData.readTimePoint(testPath, false);

		long begin = System.currentTimeMillis();
		Feature feature = extractFeature.getFeature(rawdatas, config.getThreshold_window(), config.getProbability(),
				config.getInterval());

		classifierSummary.classifer(feature, trainpoints, testpoints); //burst classifier
		//classifierSummary.paaclassifer(feature, trainpoints, testpoints); //PAA classifier

		long end = System.currentTimeMillis();
		logger.info("Run for " + (end - begin) / (double) 1000 + "seconds.");

		for (int i = 0; i < datasets.length; i++) {
			String filePath = path + "data\\" + datasets[i];
			List<DataItem> rawdata = readData.readDataFromFile(filePath, true);
			rawdatas.addAll(rawdata);
		}
	}

	public void testOnExplosion(int segment) throws Exception {
		Config config = new GetConfig().getConfig();
		logger.info(
				"==================================================================================================================");
		logger.info(config);
		logger.info(
				"===================================================================================================================");
		ReadData readData = new ReadData();
		ExtractFeature extractFeature = new ExtractFeature();
		ClassifierSummary classifierSummary = new ClassifierSummary();

		String path = config.getPath();

		int[] datasets = new int[] { 103,148,183,260,293,424,456,511,721,850,878,952,1168,1468,1570,1788,1814,1874,1881,1896,2064,2067,2108,2240,2433,2469,2484,2664,2760,2814,2988,3011,3067,3130,3207,3275,3305,3306,3534,3539,3541,3551,3577,3606,3698,3718,3741,3844,3961,3982,4001,4022,4303,4304,4318,4564,4611,4677,4810,4907,4955,5042,5166,5481,5911,5943,5946,5947,5949,5988,6008,6064,6102,6118,6119,6121,6157,6283,6302,6328,6344,6385,6405,6435,6514,6582,6594,6604,6709,6768,6775,6779,6791,6799,6801,6862,6920,6946,6962,6971,6976,6998,7053,7128,7241,7265,7288,7310,7316,7369,7387,7414,7417,7474,7476,7520,7530,7533,7537,7541,7543,7553,7567,7570,7574,7664,7665,7715,7740,7765,7770,7794,7799,7888,7889,7899,7900,7903,7909,7910,7911,8076,8256,8273,8325,8330,8540,8572,8669,8832,8983,9145,9252,9307,9825,9831,10184,10269,10831,11063,11130,11136,11313,11345,11512,11518,11830,11868,11960,11962,12192,12515,12555,12708,12721,12764,12807,12825,12987,13014,13113,13665,13684,13845,13847,13866,13939,13962,14108,14197,14285,14330,14364,14663,14790,14798,14805,14930,15201,15313
		};

		List<DataItem> rawdatas = new ArrayList<>();

		for (int i = 0; i < datasets.length; i++) {
			String filePath = path + "data\\" + datasets[i];
			List<DataItem> rawdata = readData.readDataFromFile(filePath, true);
			rawdatas.addAll(rawdata);
		}

		String trainPath = path + "train.txt";
		List<List<Long>> trainpoints = readData.readTimePoint(trainPath, false);
		String testPath = path + "test.txt";
		List<List<Long>> testpoints = readData.readTimePoint(testPath, false);

		long begin = System.currentTimeMillis();
		Feature feature = extractFeature.getFeature(rawdatas, config.getThreshold_window(), config.getProbability(),
				config.getInterval());

		//classifierSummary.classifer(feature, trainpoints, testpoints); //burst classifier
		classifierSummary.paaclassifer(feature, trainpoints, testpoints, segment); //PAA classifier with segment number set by hand

		long end = System.currentTimeMillis();
		logger.info("Run for " + (end - begin) / (double) 1000 + "seconds.");

		for (int i = 0; i < datasets.length; i++) {
			String filePath = path + "data\\" + datasets[i];
			List<DataItem> rawdata = readData.readDataFromFile(filePath, true);
			rawdatas.addAll(rawdata);
		}
	}

	public void testOnExplosionWithSrd(int segment) throws Exception {
		Config config = new GetConfig().getConfig();
		logger.info(
				"==================================================================================================================");
		logger.info(config);
		logger.info(
				"===================================================================================================================");
		ReadData readData = new ReadData();
		ExtractFeature extractFeature = new ExtractFeature();
		ClassifierSummary classifierSummary = new ClassifierSummary();

		String path = config.getPath();

		int[] datasets = new int[] { 103,148,183,260,293,424,456,511,721,850,878,952,1168,1468,1570,1788,1814,1874,1881,1896,2064,2067,2108,2240,2433,2469,2484,2664,2760,2814,2988,3011,3067,3130,3207,3275,3305,3306,3534,3539,3541,3551,3577,3606,3698,3718,3741,3844,3961,3982,4001,4022,4303,4304,4318,4564,4611,4677,4810,4907,4955,5042,5166,5481,5911,5943,5946,5947,5949,5988,6008,6064,6102,6118,6119,6121,6157,6283,6302,6328,6344,6385,6405,6435,6514,6582,6594,6604,6709,6768,6775,6779,6791,6799,6801,6862,6920,6946,6962,6971,6976,6998,7053,7128,7241,7265,7288,7310,7316,7369,7387,7414,7417,7474,7476,7520,7530,7533,7537,7541,7543,7553,7567,7570,7574,7664,7665,7715,7740,7765,7770,7794,7799,7888,7889,7899,7900,7903,7909,7910,7911,8076,8256,8273,8325,8330,8540,8572,8669,8832,8983,9145,9252,9307,9825,9831,10184,10269,10831,11063,11130,11136,11313,11345,11512,11518,11830,11868,11960,11962,12192,12515,12555,12708,12721,12764,12807,12825,12987,13014,13113,13665,13684,13845,13847,13866,13939,13962,14108,14197,14285,14330,14364,14663,14790,14798,14805,14930,15201,15313
		};

		List<DataItem> rawdatas = new ArrayList<>();

		for (int i = 0; i < datasets.length; i++) {
			String filePath = path + "data\\" + datasets[i];
			List<DataItem> rawdata = readData.readDataFromFile(filePath, true);
			rawdatas.addAll(rawdata);
		}

		String trainPath = path + "train.txt";
		List<List<Long>> trainpoints = readData.readTimePoint(trainPath, false);
		String testPath = path + "test.txt";
		List<List<Long>> testpoints = readData.readTimePoint(testPath, false);

		long begin = System.currentTimeMillis();
		Feature feature = extractFeature.getFeature(rawdatas, config.getThreshold_window(), config.getProbability(),
				config.getInterval());

		//classifierSummary.classifer(feature, trainpoints, testpoints); //burst classifier
		//classifierSummary.paaclassifer(feature, trainpoints, testpoints, segment); //PAA classifier with segment number set by hand
		classifierSummary.srdclassifer(feature,trainpoints, testpoints, segment);

		long end = System.currentTimeMillis();
		logger.info("Run for " + (end - begin) / (double) 1000 + "seconds.");

		for (int i = 0; i < datasets.length; i++) {
			String filePath = path + "data\\" + datasets[i];
			List<DataItem> rawdata = readData.readDataFromFile(filePath, true);
			rawdatas.addAll(rawdata);
		}
	}
}
